<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import chartResize from '../mixins/chartsResize'
import { getWeekStatistics } from '../../api/statics/index'
const echarts = require('echarts')

export default {
  mixins: [chartResize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data () {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    }
  },
  mounted () {
    this.getData()
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el)
      this.setOptions()
    },
    setOptions () {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['本周', '上周'],
          orient: 'vertical',
          right: 10,
          top: 150,
          itemHeight: 8,
          fontSize: 10,
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          show: true,
          left: '8%',
          right: '20%',
          bottom: '3%',
          containLabel: true,
          borderColor: 'rgba(55, 165, 240)'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false, // 坐标轴两边留白策略 默认为 true，这时候刻度只是作为分隔线，标签和数据点都会在两个刻度之间的带(band)中间
          splitArea: {
            show: true,
            areaStyle: {
              color: ['#1E314C', '#1E2E46'],
              opacity: 0.5
            }
          },
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            // 上周
            name: '上周',
            type: 'line',
            itemStyle: {
              color: '#0473eb',
              lineStyle: {
                color: '#0473eb',
                type: 'solid'
              }
            },
            smooth: true,
            data: []
          },
          {
            // 本周
            name: '本周',
            type: 'line',
            itemStyle: {
              color: 'rgb(151,51,51)',
              lineStyle: {
                color: 'rgb(151,51,51)'
              }
            },
            smooth: true,
            data: []
          }]
      })
    },
    getData () {
      getWeekStatistics()
        .then((res) => {
          console.log(res)
          // 上周数据数组
          var lastWeek = res.data.data.lastWeek
          // 本周数据数组
          var thisWeek = res.data.data.thisWeek
          // 把上周的数据取出后存入数组
          console.log(lastWeek)
          var LastWeekArray = []
          var ThisWeekArray = []
          lastWeek.map(function (item) {
            LastWeekArray.push(item.count)
          })
          console.log(LastWeekArray)
          thisWeek.map(function (item) {
            ThisWeekArray.push(item.count)
          })
          this.chart.setOption({
            series: [
              {
                name: '上周',
                data: LastWeekArray
              },
              {
                name: '本周',
                data: ThisWeekArray
              }
            ]
          })
          console.log(ThisWeekArray)
        })
        .catch((error) => {
          console.log('请求失败')
          console.log(error)
        })
    }
  }
}
</script>
